package shc.graduation.http.getter;

import android.text.TextUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import shc.graduation.bean.PaoWangArticle;

/**
 * Created by shuhuiChen on  2016/5/21  18:10.
 * Email ：shuhuiCSH@126.com
 */
public class PaoWangArticleGetter extends BaseGetter<PaoWangArticle> {

    @Override
    public List<PaoWangArticle> getModel(String websiteUrl) {

        String webContent = mIHttpGet.getContent(websiteUrl);

        if (TextUtils.isEmpty(webContent)) {
            return null;
        }

        mListModel = new ArrayList<>();

        Document doc = Jsoup.parse(webContent);
        Elements trs = doc.select("li[class=archive-item clearfix]");

        iteratorPaoWang(trs);

        return mListModel;
    }

    private void iteratorPaoWang(Elements trs) {

        PaoWangArticle paoWangArticle;
        Elements elements = null;
        boolean isCanSave;
        for (Element tr : trs) {

            paoWangArticle = new PaoWangArticle();

            elements = tr.select("div[class=covercon]");
            parseImage(paoWangArticle, elements);

            elements = tr.select("div[class=archive-detail]");
            isCanSave = parseArticle(paoWangArticle, elements);
            parseUserAddTag(paoWangArticle, elements);
            parseTime(paoWangArticle, elements);

            if (isCanSave) {
                mListModel.add(paoWangArticle);
            }
        }
        if (trs != null && trs.size() > 0) {
            trs.clear();
        }
    }

    private void parseImage(PaoWangArticle article, Elements eles) {
        if (eles == null || eles.size() <= 0) {
            return;
        }

        Element element = eles.get(0);

        Elements elements = element.getElementsByTag("img");

        if (elements == null || elements.size() <= 0) {
            return;
        }

        String photoUrl = elements.get(0).attr("src");

        if (TextUtils.isEmpty(photoUrl)) {
            return;
        }

        article.setPhotoUrl(photoUrl);
    }

    private boolean parseArticle(PaoWangArticle article, Elements eles) {

        boolean isCanSave = false;

        if (eles == null || eles.size() <= 0) {
            return isCanSave;
        }

        Element element = eles.get(0);

        Elements elements = element.getElementsByTag("h3");

        if (elements == null || elements.size() <= 0) {
            return isCanSave;
        }

        elements = elements.get(0).getElementsByTag("a");

        if (elements == null || elements.size() <= 0) {
            return isCanSave;
        }

        String contentUrl = elements.get(0).attr("href");

        if (!TextUtils.isEmpty(contentUrl)) {
            isCanSave = true;
        }

        article.setContentUrl(contentUrl);
        article.setTitle(elements.get(0).text().toString() + "");

        elements = element.getElementsByTag("p");

        if (elements != null && elements.size() > 0) {
            article.setContent(elements.get(0).text().toString() + "");
        }

        return isCanSave;
    }

    private void parseUserAddTag(PaoWangArticle article, Elements eles) {
        if (eles == null || eles.size() <= 0) {
            return;
        }

        Element element = eles.get(0);

        Elements elements = element.select("div[class=archive-info clearfix]");

        if (elements == null || elements.size() <= 0) {
            return;
        }

        element = elements.get(0);

        elements = element.select("li[class=list-user]");
        if (elements != null && elements.size() > 0) {
            Element element1 = elements.get(0);

            elements = element1.getElementsByTag("strong");
            if (elements != null && elements.size() > 0) {
                article.setFromAuthor(elements.get(0).text().toString() + "");
            }
        }


        elements = element.select("li[class=list-tag]");
        if (elements != null && elements.size() > 0) {

            elements = elements.get(0).getElementsByTag("span");

            if (elements == null || elements.size() <= 0) {
                return;
            }

            elements = elements.get(0).getElementsByTag("a");

            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0, size = elements.size(); i < size; i++) {
                stringBuilder.append(elements.get(i).text());
                if (i < (size - 1)) {
                    stringBuilder.append(",");
                }
            }
            if (!TextUtils.isEmpty(stringBuilder.toString())) {
                article.setTagList(stringBuilder.toString());
            }
        }
    }

    private void parseTime(PaoWangArticle article, Elements eles) {
        if (eles == null || eles.size() <= 0) {
            return;
        }

        Element element = eles.get(0);

        Elements elements = element.select("div[class=archive-data]");
        if (elements == null || elements.size() <= 0) {
            return;
        }

        elements = elements.get(0).select("span[class=glyphicon-class]");
        if (elements == null || elements.size() <= 0) {
            return;
        }

        article.setPublishDate(elements.get(0).text().toString() + "");
    }

    @Override
    public String getEncode() {
        return "GB2312";
    }

}
